package com.gulimall.product.app;

import java.util.Arrays;
import java.util.Map;

import com.common.valid.AddGroup;
import com.common.valid.UpdateGroup;
import com.common.valid.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gulimall.product.entity.BrandEntity;
import com.gulimall.product.service.BrandService;
import com.common.utils.PageUtils;
import com.common.utils.R;


/**
 * 品牌
 *
 * @author Ren
 * @email jackren546@gmail.com
 * @date 2024-12-31 13:14:01
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    public R save(@Validated(AddGroup.class) @RequestBody BrandEntity brand) {
    /*
            BindingResult result 校验失败发生的异常信息存储在该对象
            现在异常同意由 异常处理器处理

            if (result.hasErrors()) {

            // 1.获取校验失败信息
            HashMap<String, String> resultMap = new HashMap<>();

            // 2.封装校验失败的信息到Map
            result.getFieldErrors().forEach(error -> {
                String defaultMessage = error.getDefaultMessage();
                String field = error.getField();
                resultMap.put(field, defaultMessage);
            });

            // 3.返回失败信息的Map
            return R.error(400, "提交的数据不合法").put("data", resultMap);
        } else {

            brandService.save(brand);

            return R.ok();
        }*/

        brandService.save(brand);

        return R.ok();
    }

    @RequestMapping("/update")
    @Transactional
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand) {

        brandService.updateById(brand);
        brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update/status")
    //@RequiresPermissions("product:brand:update")
    public R updateStatus(@Validated(UpdateStatusGroup.class) @RequestBody BrandEntity brand) {

        brandService.updateById(brand);

        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
